import { createSlice } from '@reduxjs/toolkit'
import { useSelector } from 'react-redux'
import {
    addHospSet,
    batchDeleteHospSet,
    deleteHospSet,
    getHospDetail,
    getHospList,
    getHospSetList,
    lockHospList,
    lockHospSet,
    updateHospSet
} from '@/api/hosp'
import { upPageInfo } from '@/store/slice/pageCommon'

const hospSlice = createSlice({
    name: 'hosp',
    initialState: {
        hospSetList: [], // 医院设置列表
        hospitalList: localStorage.current ? localStorage.current : [], // 医院列表
        // 搜索参数
        searchData: {},
        // 医院详情
        hospDetail: {
            hospital: {
                param: {}
            },
            bookingRule: {
                rule: []
            }
        }
    },
    reducers: {
        // 更新医院设置分页列表状态
        upHospSetList(state, { payload }) {
            state.hospSetList = payload
        },
        // 更新锁定状态
        upLockHospSet(state, { payload }) {
            console.log(state, payload)
            const info: any = state.hospSetList.find((item: any) => {
                return item.id === payload
            })
            info.status = info.status === 0 ? 1 : 0
        },
        // 更新医院列表
        upHospitalList(state, { payload }) {
            state.hospitalList = payload
        },
        // 更新搜索参数
        upSearchData(state, { payload }) {
            state.searchData = payload
        },
        // 更新医院详情
        upHospDetail(state, { payload }) {
            state.hospDetail = payload
        }
    }
})
export const { upHospSetList, upLockHospSet, upHospitalList, upSearchData, upHospDetail } = hospSlice.actions
// 异步获取医院设置分页列表
export const getHospSetListAsync = (page = 1, limit = 5) => {
    return async (dispatch: any, getState: any) => {
        const { searchData } = getState().hosp
        const { data } = await getHospSetList(page, limit, searchData)
        dispatch(upHospSetList(data))
        dispatch(
            upPageInfo({
                current: data.current,
                pageSize: data.size,
                total: data.total
            })
        )
    }
}
// 异步锁定状态
export const lockHospSetAsync = (id: number, status: number) => {
    return async (dispatch: any, getState: any) => {
        await lockHospSet(id, status)
        const { current, pageSize } = getState().pageCommon.pageInfo
        // 当前页更新状态
        dispatch(getHospSetListAsync(current, pageSize))
    }
}

// 异步添加医院设置或修改医院设置
export const addOrUpdateHospSetAsync = (data: any) => {
    return async (dispatch: any, getState: any) => {
        if (data.id) {
            await updateHospSet(data)
        } else {
            await addHospSet(data)
        }
        // 重新获取医院设置分页列表
        const { current, pageSize } = getState().pageCommon.pageInfo
        dispatch(getHospSetListAsync(current, pageSize))
    }
}
// 异步删除医院设置
export const deleteHospSetAsync = (id: number) => {
    return async (dispatch: any, getState: any) => {
        await deleteHospSet(id)
        const { current, pageSize } = getState().pageCommon.pageInfo
        // 当前页更新状态
        dispatch(getHospSetListAsync(current, pageSize))
    }
}

// 异步批量删除医院设置
export const batchDeleteHospSetAsync = (idList: any) => {
    return async (dispatch: any, getState: any) => {
        await batchDeleteHospSet(idList)
        const { current, pageSize } = getState().pageCommon.pageInfo
        // 当前页更新状态
        dispatch(getHospSetListAsync(current, pageSize))
    }
}

// 异步获取医院列表
export const getHospitalListAsync = (page: number, limit: number) => {
    return async (dispatch: any, getState: any) => {
        const { data } = await getHospList(page, limit, getState().hosp.searchData)
        dispatch(upHospitalList(data))
        dispatch(
            upPageInfo({
                current: data.number + 1,
                pageSize: data.size,
                total: data.totalElements
            })
        )
        localStorage.setItem('current', data.number + 1)
    }
}
// 异步医院列表上线状态
export const lockHospListAsync = (id: string, status: number) => {
    return async (dispatch: any, getState: any) => {
        await lockHospList(id, status)
        const { current, pageSize } = getState().pageCommon.pageInfo
        // 当前页更新状态
        dispatch(getHospitalListAsync(current, pageSize))
    }
}
// 异步获取医院详情
export const getHospDetailAsync = (id: string) => {
    return async (dispatch: any) => {
        const { data } = await getHospDetail(id)
        dispatch(upHospDetail(data))
    }
}

export const useSelectorHosp = () => useSelector((state: any) => state.hosp)
export default hospSlice.reducer
